<?php
require 'include.php';
$equipe = (!empty($_REQUEST['equipe'])) ? $_REQUEST['equipe'] : "";
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
$nav = (!empty($_REQUEST['nav'])) ? $_REQUEST['nav'] : "resume";
if(!$equipe){
	echo "<h2>Les &eacute;quipes</h2>\n";
	$equipes = liste('equipes',"nom","num");
	echo "<ul class = 'nav_buttons'>";
	foreach($equipes as $id_equipe => $nom_equipe)	{
		echo "<li style = 'margin-bottom: 20px;'><button class = 'big' onclick = 'equipes(\"$id_equipe\")' style = ' padding-left:0px;text-indent: -1em;font-size: 11pt; text-align: center;font-weight: bold; '>$nom_equipe</button></li>\n";
	}
	echo "</ul>\n";

}
else{
	$equipe = new equipe($equipe);
	if($action == 'resume'){
		echo "<h3><span class = 'bold'>$equipe->nom: </span>$equipe->nom_complet</h3>\n";
		echo "<div class = 'box' >\n";
		echo leftCol("Responsable",40);
		$responsable = new membre($equipe->responsable);
		echo rightCol("<a href = 'javascript:nav_back(\"$equipe->id\",\"$equipe->nom\"); membre_detail($responsable->id);'>".$responsable->prenom." ".$responsable->nom."</a>",59);
		$s = (count($equipe->entraineurs)> 1) ? "s" : "";
		echo leftCol("<span class = ''>Entraineur$s</span>",40);
		$print_entr = array();
		foreach($equipe->entraineurs as $entr_id => $entr_nom){
			$print_entr[] = "<a href = 'javascript:nav_back(\"$equipe->id\",\"$equipe->nom\"); membre_detail($entr_id);'>".$entr_nom."</a>";
		}
		echo rightCol(implode("<br />",$print_entr),59);
		$s = (count($equipe->entrainements)> 1) ? "s" : "";
		echo leftCol("<span class = ''>Entrainement$s</span>",40);
		$entr = "<ul style = 'list-style: none;text-indent: 0; margin-top: 0; padding-top: 0; margin-left: 0; padding-left:0'>";
		$q = "select * from entrainements where equipe_id = '$equipe->id' and `match` = 0";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$salle = new salle($s->salle);
			$entr .= "<li>".$jours[$s->jour]." ".$s->heure."<br /> ".(ucwords(strtolower(str_ireplace('salle','',$salle->lien("0")))))."</li>";
		}
		$entr.= "</ul>\n";
		echo rightCol($entr,59);
		#### matchss ####
		echo leftCol("<span class = ''>matchs domicile</span>",40);
		$entr = "<ul style = 'list-style: none;text-indent: 0; margin-top: 0; padding-top: 0; margin-left: 0; padding-left: 0'>";
		$q = "select * from entrainements where equipe_id = '$equipe->id' and `match` = 1";
		$r = ExecRequete($q);
		while($s = LigneSuivante($r)){
			$salle = new salle($s->salle);
			$entr .= "<li>".$jours[$s->jour]." ".$s->heure."<br /> ".(ucwords(strtolower(str_ireplace("salle","",$salle->lien("0")))))."</li>";
		}
		$entr.= "</ul>\n";
		echo rightCol($entr,59);

		echo "<br class = 'clear' />\n";
		echo"</div>\n";
	}
	elseif($action == "joueurs"){
		$joueurs = (strpos($equipe->nom,"F") !== FALSE) ? "joueuses" : "joueurs";
		echo "<h3><span class = 'bold'>$equipe->nom: </span>$joueurs</h3>\n";
		echo "<div class = 'box'>\n";
		$q = "select membre_id, nom, prenom from membres inner join membre_equipes on membres.id = membre_id where equipe_id = '$equipe->id' order by nom, prenom";
		$r  = ExecRequete($q);
		$lines= array();
		while($s = LigneSuivante($r)){
			ob_start();
			$joueur = (strlen($s->prenom." ".$s->nom) > 30) ? substr($s->prenom." ".$s->nom,0,28)."..." : $s->prenom." ".$s->nom;
			echo "<div style = 'width: 240px; text-align: left; position: relative;' onclick = 'nav_back(\"$equipe->id\",\"$equipe->nom\"); membre_detail($s->membre_id);'>$joueur <span class = 'float_right'>&gt;</span></div>\n";
			$lines[] = ob_get_clean();
		}
		if(count($lines)){
			echo implode("<br class = 'clear' /><hr style = 'margin-left: -10px; margin-right: -10px; '/>\n",$lines);
			echo "<br class = 'clear' />\n";
		}
		echo "</div>\n";
	}
	elseif($action == 'matchs'){
		echo "<h3><span class = 'bold'>$equipe->nom: </span>matchs</h3>\n";
		echo "<p class = 'center fade small'>cliquez sur un match pour afficher le d&eacute;tail</p>\n";
		$matchs = $equipe->matchs();
		$table = new table("matchs");
		$fields = array("date" => "date",
						"match" => "match",
						"info" => "info");
		foreach($fields as $field => $print){
			$$field = new table_field($field,'matchs');
			$$field->print = $field;
			if($field == 'info') $$field->styleclass = 'center';
			$table->fields[] = $$field;
		}
		foreach($matchs as $match){
			$row = new table_row();
			foreach($fields as $field){
				if($field == 'match'){
					if(stripos($match->local,"LUTRY") !== FALSE) $row->cols['match'] = "VBCLL - <span class = 'bold'>$match->visiteur</span>";
					else $row->cols['match'] = "<span class = 'bold'>$match->local</span> - VBCLL";
				}
				elseif($field == 'date'){
					$print_date = (ereg("[a-z]+",format_date($match->date,'d.m.y')) !== FALSE) ? "" : $match->jour;
					$print_date .= " ".format_date($match->date,'d.m.y');
					$row->cols['date'] = $print_date;
				}
				elseif($field == 'info'){
					if($match->resultat) $row->cols['info'] = $match->format_resultat();
						elseif($match->date <= date("Y-m-d")&&(str_replace(":","",$match->heure) > date("His"))){
						if($user_id = check_cookie()){
							$user = new membre($user_id);
							if((in_array($match->equipe,$user->equipes))||(check_admin())){
								$content = "<select onchange = 'score_match(\"$match->no\",this.options[this.selectedIndex].value)'>";
								$results = array("3-0","3-1","3-2","2-3","1-3","0-3");
								foreach($results as $result) $content .= "<option value = '$result'>$result</option>";
								$content .= "</select>";
							}
							else $content = "<span class = 'fade'>??</span>";
						}
						else $content = "<span class = 'fade'>??</span>";
						$row->cols['info'] = $content;
					}
					else $row->cols['info'] = $match->heure;
				}
			}
			foreach($row->cols as $col_id => $col_val){
				if(strpos($col_val,"<select") === FALSE) $row->cols[$col_id] = "<span onclick = 'match_detail(\"$match->no\",$equipe->id,\"$equipe->nom\")'>$col_val</span>";
			}
			$table->rows[] = $row;
		}
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer();
	}
	elseif($action == 'photo'){
		echo "<h3><span class = 'bold'>$equipe->nom: </span>$equipe->nom_complet</h3>\n";
		echo "<p class = 'center'>".$equipe->photo_iphone()."</p>\n";
	}
	elseif($action == 'communications'){
		echo "<h3><span class = 'bold'>$equipe->nom: </span>communications</h3>\n";
		if(check_cookie()){
			if($user_id = check_cookie()){
				$user = new membre($user_id);
				if(array_key_exists($equipe->id,$user->equipes)){
					echo "<p class = 'center'><button type = 'button' onclick = 'form_communication(\"$equipe->id\")'>ajouter une communication</button></p>\n";
				}
			}
			echo "<div class = 'box' id = 'communications'>\n";
			$q = "select corps, date, concat(prenom,' ',nom) as auteur, membre_id, communications.id as id from communications inner join membres on (communications.membre_id = membres.id) where equipe_id = $equipe->id order by date desc";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$del = (($s->membre_id == $user_id)||(check_admin())) ? "<span class = 'float_right'>&nbsp;<img src = 'images/icon_delete.png' alt = 'supprimer' width = '12' height = '12' onclick = 'if(confirm(\"Confirmer la suppression de la communication ?\")) delete_communication($s->id,$equipe->id)' style = 'cursor:pointer;' /></span>" : "";
				echo "<p class = 'fade separation'><a href = 'javascript: membre_detail($s->membre_id)'>$s->auteur</a>, ".format_date($s->date).$del."</p>\n";
				echo "<p>".(stripslashes($s->corps))."</p>";
				echo "<hr style = 'margin-left: -10px; margin-right: -10px'/>\n";
			}
			if(!mysql_num_rows($r)) echo "<p class = 'center fade'>aucune communication</p>\n";
			echo "</div>\n";
		}
		else{
			echo "<p class = 'center fade'><a href = 'index.php'>identifiez-vous</a></p>\n";
		}
	}
	else{
		$buttons = array("resume" => "icon_resume",
						 "photo" => "icon_photo",
						 "joueurs" => "icon_equipe",
						 "matchs" => "icon_calendrier",
						 "communications" => "icon_chat");
		echo "<ul class = 'nav_buttons'>";
		foreach($buttons as $action => $icon){
			echo "<li><button class = 'big' onclick = 'equipe_detail(\"$equipe->id\",\"$action\")'><img src = 'images/$icon.png' alt = '$action' height = '40' width = '40'  style = 'margin-left: -18px'/></button></li>\n";
		}
		echo "</ul>\n";
		echo "<br class = 'clear' />\n";
		echo "<div id = 'action_equipe'>\n";
		echo "<h3><span class = 'bold'>$equipe->nom: </span>$equipe->nom_complet</h3>\n";
		echo "</div>\n";
	}
}
?>